package com.example.myapplication15_3;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText etName, etScore, etSearch;
    private Button btnSave, btnSearch;
    private RecyclerView recyclerView;
    private StudentAdapter adapter;
    private StudentDataManager dataManager;
    private List<Student> studentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化数据管理器
        dataManager = new StudentDataManager(this);
        
        // 初始化视图
        initViews();
        
        // 设置点击事件
        setupClickListeners();
        
        // 加载数据
        loadStudentData();
    }

    private void initViews() {
        etName = findViewById(R.id.etName);
        etScore = findViewById(R.id.etScore);
        etSearch = findViewById(R.id.etSearch);
        btnSave = findViewById(R.id.btnSave);
        btnSearch = findViewById(R.id.btnSearch);
        recyclerView = findViewById(R.id.recyclerView);

        // 设置RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        
        // 添加分割线
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(
                recyclerView.getContext(),
                layoutManager.getOrientation()
        );
        recyclerView.addItemDecoration(dividerItemDecoration);
        
        adapter = new StudentAdapter(studentList);
        recyclerView.setAdapter(adapter);
    }

    private void setupClickListeners() {
        // 保存按钮点击事件
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveStudentData();
            }
        });

        // 搜索按钮点击事件
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchStudents();
            }
        });
    }

    private void loadStudentData() {
        // 加载所有学生数据
        studentList = dataManager.loadAllStudents();
        adapter.updateData(studentList);
    }

    private void saveStudentData() {
        // 获取用户输入
        String name = etName.getText().toString().trim();
        String scoreStr = etScore.getText().toString().trim();

        // 验证输入
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(scoreStr)) {
            Toast.makeText(this, "请输入分数", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            float score = Float.parseFloat(scoreStr);
            
            // 创建学生对象
            Student student = new Student(name, score);
            
            // 保存到JSON文件
            boolean success = dataManager.saveStudent(student);
            
            if (success) {
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                
                // 清空输入框
                etName.setText("");
                etScore.setText("");
                
                // 重新加载数据
                loadStudentData();
            } else {
                Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
            }
            
        } catch (NumberFormatException e) {
            Toast.makeText(this, "分数格式不正确", Toast.LENGTH_SHORT).show();
        }
    }

    private void searchStudents() {
        // 获取搜索关键字
        String keyword = etSearch.getText().toString().trim();
        
        // 执行搜索
        List<Student> searchResults = dataManager.searchStudentsByName(keyword);
        
        // 更新列表
        adapter.updateData(searchResults);
        
        // 提示搜索结果
        if (searchResults.isEmpty()) {
            Toast.makeText(this, "没有找到匹配的学生", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "找到 " + searchResults.size() + " 个匹配的学生", Toast.LENGTH_SHORT).show();
        }
    }
}